<?php
namespace Admin\Controller;

class ArticleController extends BaseController {
    public function __construct()
    {
        $this->setWebTitle('博客文章')
            ->setIdentifier('article');
        //     ->setLicense(false)
        //     ->addCrumb(L('index'), U('Home/Index/index'));

        parent::__construct();
    }

    // 首页
    public function indexAction()
    {
        $categorys = M('category')->select();
        $this->assign(array('identifier' => 'article.art', 'categorys' => $categorys));

        $this->display('index');
    }

    public function pageAction()
    {
        if(IS_GET){
            $current_page = I('get.page/d');
            $num = I('get.limit/d');;

            //加载分页
            $total_page = M('Article')->where("admin_id=1")->count();
            $results = D('Article')->pages($current_page, $num, array('id'=>'asc'), array('admin_id'=>1));

            $data = array(
                'code'  => 0,
                'msg'   => '',
                'count' => $total_page,
                'data'  => $results
            );
            $this->ajaxReturn($data, 'json');
        }
        return $this->error404Page();
    }

    public function addArticleAction()
    {
        if(IS_POST){

            $data = I('post.data', '');
            // $catid = I('post.catid', '');
            // $tag = I('post.tag', '');
            // $content = I('post.content', '');

            $error = '';
            $success = D('Article')->add($data, $error);

            if ($success) {
                $this->successAjax('文章添加成功,等待审核。。。');
            } else {
                $this->errorAjax($error);
            }    
        }
    }

    public function editArticleAction()
    {
        if(IS_POST){
            $data = I('post.data', '');

            $error = '';
            if (count($data) == count($data, 1)) {
                $success = D('Article')->update($data, $error);
            } else {
                foreach ($data as $da) {
                    $success = D('Article')->update($da, $error);
                }
            }

            if ($success) {
                $this->successAjax('文章更新成功');
            } else {
                $this->errorAjax($error);
            }    
        }
    }

    public function deleteArticleAction()
    {
        if(IS_POST){

            $data = I('post.data', '');

            $error = '';
            if(count($data) == count($data, 1)){
                $success = D('Article')->delete($data['id'], $data['catid'], $error);
            }else{
                foreach ($data as $da) {
                    $success = D('Article')->delete($da['id'], $da['catid'],$error);
                }
            }

            if ($success) {
                $this->successAjax('删除文章成功');
            } else {
                $this->errorAjax($error);
            }    
        }
    }

    public function findOneAction()
    {
        if(IS_POST){
            $id = I('post.id', '');

            $error = '';
            $data = D('Article')->findOne($id, $error);

            if ($data) {
                $this->successAjax($data);
            } else {
                $this->errorAjax($error);
            }    
        }
    }
}